﻿using System;
using System.Linq;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Reviewer.Entities.Services;
using Reviewer.Business;
using Reviewer.Entities;


public partial class public_html_NewSite_CMSV2_UserEdit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            using (IWriterService service = ServiceFactory.WriterService)
            {
                Writer writer = service.Find().FirstOrDefault(t => t.LogOnName.ToUpperInvariant().Equals(Page.User.Identity.Name.ToUpperInvariant(), StringComparison.Ordinal));
                if (writer != null)
                {
                    txtSpiller.Text = writer.Playing;
                }
            }

            MembershipUser currentUser = Membership.GetUser();
            txtOm.Text = currentUser.Comment;
            txtEmail.Text = currentUser.Email;
        }
    }

    public void BtmUpdate_Click(object sender, EventArgs e)
    {
        using (IWriterAdminService adminService = ServiceFactory.WriterAdminService)
        using (IWriterService service = ServiceFactory.WriterService)
        {
            Writer writer = service.Find(Page.User.Identity.Name).FirstOrDefault();
            if (writer == null)
            {
                writer = new Writer();
                writer.LogOnName = Page.User.Identity.Name;
            }
            writer.Playing = txtSpiller.Text;

            adminService.Save(writer);
        }

        MembershipUser currentUser = Membership.GetUser();
        currentUser.Comment = txtOm.Text;
        currentUser.Email = txtEmail.Text;

        Membership.UpdateUser(currentUser);

        Response.Redirect("Message.aspx?MSG=Din profil blev opdateret.");
    }

    protected void btnChangePassword_Click(object sender, EventArgs e)
    {
        try
        {
            Membership.GetUser().ChangePassword(tbCurrentPassword.Text, tbNewPassword.Text);
        }
        catch (Exception err)
        {
            System.Diagnostics.Trace.WriteLine(err.ToString());
            Response.Redirect("Message.aspx?MSG=Din adgangskode kunne ikke ændres.");
            return;
        }

        Response.Redirect("Message.aspx?MSG=Din adgangskode er ændret.");
    }
}
